<template>
  <div class="container">
    <h2>点击弹窗示例</h2>
    <div class="click-area" 
         ref="clickArea" 
         @click="handleClick">
      <div class="grid-background"></div>
      <span class="hint">点击任意位置查看坐标</span>
      
      <!-- 弹窗 -->
      <template v-if="popupConfig">
        <div class="popup" 
             :class="{ show: isPopupVisible }"
             :style="popupStyle"
             @click.stop>
          <div class="popup-header">
            <span>坐标信息</span>
            <i class="el-icon-close close-btn" @click="closePopup"></i>
          </div>
          <div class="popup-content">
            <div class="coordinate-group">
              <div class="label">相对坐标：</div>
              <div class="coordinates">
                <div class="coord-item">
                  <span>X: {{ popupConfig.x }}px</span>
                  <span>Y: {{ popupConfig.y }}px</span>
                </div>
              </div>
            </div>
            <div class="coordinate-group">
              <div class="label">绝对坐标：</div>
              <div class="coordinates">
                <div class="coord-item">
                  <span>X: {{ popupConfig.clientX }}px</span>
                  <span>Y: {{ popupConfig.clientY }}px</span>
                </div>
              </div>
            </div>
          </div>
        </div>
      </template>
    </div>
  </div>
</template>

<script>
export default {
  name: 'ClickPopup',
  
  data() {
    return {
      popupConfig: null,
      isPopupVisible: false
    };
  },

  computed: {
    popupStyle() {
      if (!this.popupConfig) return {};
      
      const { x, y, isTop, isRight } = this.popupConfig;
      const transform = [];

      // 处理垂直方向
      if (isTop) {
        transform.push('translateY(-100%)');
      }

      // 处理水平方向
      if (isRight) {
        transform.push('translateX(-100%)');
      }

      return {
        left: `${x}px`,
        top: `${y}px`,
        transform: transform.length ? transform.join(' ') : 'none'
      };
    }
  },

  mounted() {
    document.addEventListener('click', this.handleDocumentClick);
  },

  beforeDestroy() {
    document.removeEventListener('click', this.handleDocumentClick);
  },

  methods: {
    handleClick(e) {
      const rect = this.$refs.clickArea.getBoundingClientRect();
      const x = e.clientX - rect.left;
      const y = e.clientY - rect.top;
      
      // 计算点击位置是否靠近边缘
      const isTop = y > rect.height / 2;
      const isRight = x > rect.width / 2;

      console.log(isTop, isRight);

      this.popupConfig = {
        x,
        y,
        clientX: e.clientX,
        clientY: e.clientY,
        isTop,
        isRight
      };
      
      this.$nextTick(() => {
        this.isPopupVisible = true;
      });
    },

    closePopup() {
      this.isPopupVisible = false;
      setTimeout(() => {
        this.popupConfig = null;
      }, 300);
    },

    handleDocumentClick(e) {
      if (this.popupConfig && 
          !this.$refs.clickArea.contains(e.target)) {
        this.closePopup();
      }
    }
  }
};
</script>

<style scoped>
.container {
  padding: 20px;
}

h2 {
  color: #303133;
  margin-bottom: 20px;
  font-size: 18px;
  font-weight: 500;
}

.click-area {
  width: 100%;
  height: 500px;
  border: 1px solid #dcdfe6;
  border-radius: 4px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  position: relative;
  background: #fff;
  overflow: hidden;
}

.grid-background {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-image: 
    linear-gradient(#f0f2f5 1px, transparent 1px),
    linear-gradient(90deg, #f0f2f5 1px, transparent 1px);
  background-size: 20px 20px;
  opacity: 0.5;
  pointer-events: none;
}

.hint {
  color: #909399;
  font-size: 14px;
  z-index: 1;
}

.popup {
  position: absolute;
  background: white;
  padding: 0;
  border-radius: 4px;
  box-shadow: 0 2px 12px 0 rgba(0,0,0,.1);
  z-index: 1000;
  width: 250px;
  opacity: 0;
  transform: scale(0.9);
  transition: all 0.2s ease-in-out;
  pointer-events: none;
  border: 1px solid #ebeef5;
}

.popup.show {
  opacity: 1;
  transform: scale(1);
  pointer-events: auto;
}

.popup-header {
  padding: 12px 15px;
  border-bottom: 1px solid #ebeef5;
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 14px;
  color: #606266;
}

.close-btn {
  cursor: pointer;
  color: #909399;
  font-size: 16px;
  transition: color 0.2s;
}

.close-btn:hover {
  color: #409eff;
}

.popup-content {
  padding: 15px;
}

.coordinate-group {
  margin-bottom: 12px;
}

.coordinate-group:last-child {
  margin-bottom: 0;
}

.label {
  color: #606266;
  font-size: 13px;
  margin-bottom: 5px;
}

.coordinates {
  background: #f5f7fa;
  border-radius: 4px;
  padding: 8px 12px;
}

.coord-item {
  display: flex;
  justify-content: space-between;
  font-family: monospace;
  color: #409eff;
  font-size: 13px;
}
</style> 